<?php

class Sanpham extends CI_Controller {
    
    function __construct() {
        parent::__construct();
		$this->load->library('form_validation');		
		$this->load->helper(array('form','url','codegen_helper'));
		$this->load->model('codegen_model','',TRUE);
	}	
	
	function index(){
		$this->manage();
	}

	function manage(){
        $this->load->library('table');
        $this->load->library('pagination');
        
        //paging
        $config['base_url'] = base_url().'index.php/sanpham/manage/';
        $config['total_rows'] = $this->codegen_model->count('sanpham');
        $config['per_page'] = 10;	
        $this->pagination->initialize($config); 	
        $this->data['results'] = $this->codegen_model->get_join('sanpham','chungloai','sanpham.maloaisp = chungloai.ma_chung_loai','*','',$config['per_page'],'','');

	   $this->load->view('admin/sanpham/sanpham_list', $this->data); 
       //$this->template->load('content', 'sanpham_list', $this->data); // if have template library , http://maestric.com/doc/php/codeigniter_template
		
    }
	
    function add(){      
        $this->load->library('upload');
        $this->load->library('form_validation');    
		$this->data['custom_error'] = '';
		
        if ($this->form_validation->run('sanpham') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {   
            if (isset($_FILES)) {
                $name = $_FILES['hinh_sp']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/img_product/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinh_sp')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
                $data = array(
                    'ma_sp' => $this->input->post('ma_sp'),
					'maloaisp' => $this->input->post('maloaisp'),
					'ten_sp' => $this->input->post('ten_sp'),
					'gia' => $this->input->post('gia'),
					'gioithieu_sp' => $this->input->post('gioithieu_sp'),
					'hinh_sp' =>$name,
					'soluong_sp' => $this->input->post('soluong_sp'),
					'sp_xuhuong' => $this->input->post('sp_xuhuong'));
           
                    if ($this->codegen_model->add('sanpham',$data) == TRUE)
                    {
                            //$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
                            // or redirect
                            redirect(base_url().'index.php/sanpham/manage/');
                    }
                    else
                    {
                            $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';

                    }
		}
                $this->data['chungloai'] = $this->codegen_model->get('chungloai', '*', '', 1000,'');
		$this->load->view('admin/sanpham/sanpham_add', $this->data);   
        //$this->template->load('content', 'sanpham_add', $this->data);
    }	
    
    function edit(){        
        $this->load->library('upload');
        $this->load->library('form_validation');    
		$this->data['custom_error'] = '';
		
        if ($this->form_validation->run('sanpham') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {                
            if (isset($_FILES)) {
                $name = $_FILES['hinh_sp']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/img_product/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinh_sp')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
            if($name !=''){
                $data = array(
                    'ma_sp' => $this->input->post('ma_sp'),
					'maloaisp' => $this->input->post('maloaisp'),
					'ten_sp' => $this->input->post('ten_sp'),
					'gia' => $this->input->post('gia'),
					'gioithieu_sp' => $this->input->post('gioithieu_sp'),
					'hinh_sp' =>$name,
					'soluong_sp' => $this->input->post('soluong_sp'),
					'sp_xuhuong' => $this->input->post('sp_xuhuong')
            );
            }else{
                $data = array(
                        'ma_sp' => $this->input->post('ma_sp'),
                                            'maloaisp' => $this->input->post('maloaisp'),
                                            'ten_sp' => $this->input->post('ten_sp'),
                                            'gia' => $this->input->post('gia'),
                                            'gioithieu_sp' => $this->input->post('gioithieu_sp'),
                                            'soluong_sp' => $this->input->post('soluong_sp'),
                                            'sp_xuhuong' => $this->input->post('sp_xuhuong')
                );
            }
           
                if ($this->codegen_model->edit('sanpham',$data,'id',$this->input->post('id')) == TRUE)
                {
                        redirect(base_url().'index.php/sanpham/manage/');
                }
                else
                {
                        $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>';

                }
        }

		$this->data['result'] = $this->codegen_model->get('sanpham','id,ma_sp,maloaisp,ten_sp,gia,gioithieu_sp,hinh_sp,soluong_sp,sp_xuhuong','id = '.$this->uri->segment(3),NULL,NULL,true);
		
		$this->load->view('admin/sanpham/sanpham_edit', $this->data);		
        //$this->template->load('content', 'sanpham_edit', $this->data);
    }
	
    function delete(){
            $ID =  $this->uri->segment(3);
            $this->codegen_model->delete('sanpham','id',$ID);             
            redirect(base_url().'index.php/sanpham/manage/');
    }
}

/* End of file sanpham.php */
/* Location: ./system/application/controllers/sanpham.php */